package vsm.vsm_server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.UserMapper;
import vsm.vsm_server.mapper.VideoMapper;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.pojo.Video.Video;
import vsm.vsm_server.pojo.Video.VideoNailVO;
import vsm.vsm_server.pojo.Video.VideoUrlVO;
import vsm.vsm_server.service.SearchService;
import vsm.vsm_server.service.UserService;
import vsm.vsm_server.service.VodService;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j // 使用 Lombok 自动生成 log 字段
public class SearchServiceImpl implements SearchService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    VodService vodService;
    @Override
    public List<UserInfo> searchUser(String keyword) {
        List<UserInfo> userInfoList = userMapper.findUsersByKeyword("%" + keyword + "%");
        userInfoList = userService.userInfoListGetAvatar(userInfoList);
        return  userInfoList;
    }

    @Override
    public List<VideoNailVO> searchVideo(String keyword) {
        List<Video> videoList = videoMapper.findVideosByKeyword("%" + keyword + "%");
        List<VideoNailVO> videoNailVOList = new ArrayList<>();
        List<BigInteger> userIdList = new ArrayList<>();
        if (videoList != null) {
            for (Video v : videoList) {
                VideoNailVO newVideoNailVO = new VideoNailVO();
                BeanUtils.copyProperties(v, newVideoNailVO);
                VideoUrlVO newVideoUrlVO = vodService.getUrl(newVideoNailVO.getPath());
                BeanUtils.copyProperties(newVideoUrlVO, newVideoNailVO);
                userIdList.add(v.getUserId());
                videoNailVOList.add(newVideoNailVO);
            }
            Map<BigInteger,UserInfo> userInfoMap = userService.getUserInfoList(userIdList);
            for(VideoNailVO v:videoNailVOList){
                UserInfo userInfo = userInfoMap.get(v.getUserId());
                BeanUtils.copyProperties(userInfo,v);
            }
        }
        return videoNailVOList;
    }
}
